<?php
class Languages extends MY_Controller
{
	var $curl = 'languages';
	var $name = 'Языки';
	var $search = '';
	var $per_page = 0;
	var $language;
		
	function __construct()
	{
		parent::__construct();
				
		$this->load->model('language_model');

		$this->validation->set_fields($this->language_model->fields);

		$this->language = parent::getLang();	
	}
	
	function index()
	{		
		$this->data['Title'] = T_('Languages | Modify Languages');
		$this->language_model->search();
		//$this->search = $this->data['search'];
		//$this->per_page = $this->data['per_page'];
		//$this->data['search_box'] = $this->load->view($this->curl . '/search', null, true);		
		$this->renderLayout();
	}

	function create()
	{		
		if (count($_POST))
		{
			$rules = array('language_name', 'language_ID');
			$this->validation->set_rules($this->language_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{
				$item['language_name'] = $this->validation->language_name;
				$item['language_ID'] = $this->validation->language_ID;
				//$item['language_created'] = MYSQL_NOW();
				//$item['language_modified'] = MYSQL_NOW();
				$insert_ID = $this->language_model->insert($item);
				
				redirect($this->curl . '/index/');
			}
			else
			{
				$this->form = $this->curl . '/create';
			}
		}
		else
		{
			$this->form = $this->curl . '/create';
		}

		$this->data['Title'] = $this->name . ' / ' . T_('Create');
		$this->language_model->search();
		$this->renderLayout();		
	}
	
	function deleteItems()
	{
		$data = $this->language_model->delete($_REQUEST['items']);

		echo $this->utils->php2js($data);
	}

	function delete($id)
	{
		$data = $this->language_model->delete($id);

		redirect($this->curl . '/index/');
	}

	function activity()
	{
		$data = $this->utils->publishItems($this->language_model->table, $this->language_model->prKey, $this->language_model->publishedField, $_REQUEST['items'], $_REQUEST['action']);
		echo $this->utils->php2js($data);
	}
	
	function edit($id = null)
	{
		if (!empty($id))
		{
			$object = $this->language_model->get($id);
			$this->data['Title'] = $object['language_name'] . ' - ' . T_('Edit');
					
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
	
				return false; 
			}
		
			$this->validation->language_ID = $object['language_ID'];

			if (!count($_POST))
			{
				$this->validation->language_name = $object['language_name'];				
				$this->form = $this->curl . '/edit'; 
			}
			else
			{
				$rules = array('language_name', 'language_ID');
				$this->validation->set_rules($this->language_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['language_name'] = $this->validation->language_name;
					$item['language_ID'] = $this->validation->language_ID;
					//$item['language_modified'] = date('Y-m-d H:i:s');
					
					$this->language_model->update($item);
					redirect($this->curl . '/index/');				
				}
				else
				{
					$this->form = $this->curl . '/edit';
				}				
			}			
		}
		$this->data['Title'] = 'Languages | Modify Languages';		
		$this->renderLayout();
	}
}
?>